﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Frogism.Core
{
    public class ReportPerYear
    {
        protected IEnumerable<IBook> Books;
        public ReportPerYear(IEnumerable<IBook> books)
        {
            Books = books;
        }
        public IEnumerable<YearStat> GetStats()
        {
            var results = new List<YearStat>();
            foreach (var year in GetYearList())
            {
                var runningCount =0;
                var finished = 0;
                foreach (var b in Books)
                {
                    int after = b.ReadBy(new DateTime(year, 12, 31));
                    int before = b.ReadBy(new DateTime(year - 1, 12, 31));
                    runningCount = runningCount + (after - before);
                    if (b.IsFinished && b.GoalYear == year)
                    {
                        finished++;
                    }
                }
                results.Add(new YearStat(year,runningCount,finished,0));

            }
            return results;
        }
        public int[] GetYearList()
        {
            IEnumerable<int> yearList = new List<int>();

            foreach (var b in Books)
            {
                var years = from b2 in b.Reads
                            select b2.ReadDate.Year;
                yearList = yearList.Union(years);
            }
            return yearList.OrderBy(f=>f).ToArray();
        }
    }
    public class YearStat{
        public YearStat(int year, int pagesRead, int BooksFinished, int historicRead)
        {
            _PagesRead = pagesRead;
            _BooksFinished = BooksFinished;
            _HistoricRead = historicRead;
            _Year = year;
        }
        private int _PagesRead;
        public int PagesRead{get{return _PagesRead;}}
        private int _BooksFinished;
        private int _HistoricRead;
        public int HistoricRead { get { return _HistoricRead; } }
        public int BooksFinished{get{return _BooksFinished;}}
        private int _Year;
        public int Year { get { return _Year; } }
    }

    public class ReportPerMonth
    {
        protected IEnumerable<IBook> Books;
        public ReportPerMonth(IEnumerable<IBook> books)
        {
            Books = books;
        }
        public IEnumerable<YearStat> GetStats(int year)
        {
            var results = new List<YearStat>();
            for (int i = 1; i <= 12;i++ )
            {
                var runningCount = 0;
                var historyCount = 0;
                var finished = 0;
                foreach (var b in Books)
                {
                    var baseTime = new DateTime(year, i, 1);
                    int after = b.ReadBy(baseTime.AddMonths(1).AddDays(-1));
                    int before = b.ReadBy(baseTime.AddDays(-1));
                    runningCount = runningCount + (after - before);

                    var historicTime = new DateTime(year-1, i, 1);
                    int afer2 = b.ReadBy(historicTime.AddMonths(1).AddDays(-1));
                    int before2 = b.ReadBy(historicTime.AddDays(-1));

                    historyCount = historyCount + (afer2 - before2);


                    //if (b.IsFinished && b.GoalYear == year)
                    //{
                    //    finished++;
                    //}
                }
                results.Add(new YearStat(i, runningCount, finished, historyCount));

            }
            return results;
        }
        public int[] GetYearList()
        {
            IEnumerable<int> yearList = new List<int>();

            foreach (var b in Books)
            {
                var years = from b2 in b.Reads
                            select b2.ReadDate.Year;
                yearList = yearList.Union(years);
            }
            return yearList.OrderBy(f => f).ToArray();
        }
    }
}
